<?php
class MatriculasController extends AppController {

	var $name = 'Matriculas';
	var $components = array('RequestHandler');
	
	function index(){
		$this->layout="ajax";		
		//	var_dump($this->JQGridPaginate->isGrid);exit;	
		if($this->JQGridPaginate->isGrid){
			$fields = array('Matricula.id',
			'"Persona"."codigo" AS "Matricula__personacodigo"',
			'"Persona"."nombre_completo" AS "Matricula__persona"',
			'Matricula.fecha',
			'Matricula.monto',
			'Matricula.created',
			'"Estado"."descripcion" AS "Matricula__estado"');	
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			$this->autoRender = false;
			echo $this->JQGridPaginate->paginate("Matricula");
		}elseif ($this->RequestHandler->isPost()){	
			$this->autoRender = false;
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
				$this->Matricula->create();				 
			}elseif ($oper == "edit"){
				$this->data['Matricula']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Matricula']['id'] = $this->params['form']['id'];
				$this->data['Matricula']['estado_id'] = 2;				
			}
			
			$this->Matricula->save($this->data);			
		}		
	}
		
	function add($id = null){
		$this->layout = 'ajax';
		//$this->loadModel('Programacion');
		//if(!empty($id) && !($id === 'undefined')){
		//	$this->data = $this->Programacion->getProgramacion($id);
		//}
		$this->loadModel('Programacion');
		$listaProgramacion= $this->Programacion->find('all',array('conditions'=>array('Programacion.id'=>$id)));
		$this->set('programacion', $listaProgramacion);
		
		$listaMatricula= $this->Matricula->find('all',array('conditions'=>array('Matricula.programacion_id'=>$id)));
		$this->set('matricula', $listaMatricula);
		
		$this->data = $this->Matricula->getMatricula($id);
		//var_dump($this->data['detalles']);exit;
		/*$this->loadModel('Proveedor');
		$listaProveedores = $this->Proveedor->find('list',array('fields'=>array('Proveedor.id','Proveedor.razonsocial'),
														'order'=>'Proveedor.razonsocial'));
		$this->loadModel('Moneda');
		$listaMonedas = $this->Moneda->find('list',array('fields'=>array('Moneda.id','Moneda.nombre'),
												'order'=>'Moneda.nombre','conditions'=>array('Moneda.id'=>array(1,2))));
		$this->set('monedas', $listaMonedas);
		$this->set('proveedors', $listaProveedores);*/
	}
	
	function actualizar($id=NULL){ 
	    Configure::write('debug', 1);
	    $accion = empty($this->params['named']['oper'])?'eliminar':$this->params['named']['oper'];
		$this->layout = 'ajax';
    	
		if($accion == 'update'){ 
			$this->Matricula->begin();
			/** editamos o agregamos **/
			$respuesta = $this->Matricula->setGuardarMatricula($this->data,$this->variables);
			if($respuesta['respuesta']){
				$this->Matricula->commit();
				echo json_encode($respuesta);
			}else{
				$this->Matricula->rollback();
				echo json_encode($respuesta);
			}
		}else{
			/** Desactivamos la orden de compra **/
			if(!empty($this->data['Matricula']['id'])){
				$ordencompra['Matricula']['id'] = $this->data['Matricula']['id'];
				$ordencompra['Matricula']['estado_id'] = 2;
				@$this->Matricula->save($ordencompra['Matricula']);
				echo json_encode($this->Matricula->visualizarError($this->Matricula->validationErrors));
			}
		}
		$this->autoRender = false;
	}	
}